package com.sunjob.mroresumeproject.busactioncontroller;


import com.sunjob.common.domain.mroresume.CKBus;
import com.sunjob.common.domain.mroresume.CKParts;
import com.sunjob.common.domain.mroresume.TFBus;
import com.sunjob.common.result.Result;
import com.sunjob.common.util.MROUtils;
import com.sunjob.mroresumeproject.service.CKBusService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/CK")
@CrossOrigin
@Api(tags = "机车定检测试接口")
public class CKBusController {

    @Autowired
    CKBusService ckBusService;

    //分页，上下页
    @RequestMapping("/getpages")
    public Result getpages(@RequestParam(value = "pages2")String pages2,String types){

        int page2=Integer.parseInt(pages2);

        if(page2<1){
            page2=1;
        }



        int size=5;
        //部件定检履历查出部件定检记录总条数
        System.out.println(types);
        int count=ckBusService.querycount(types);
        System.out.println(count);
        //求总页数
        int pageCount =((count-1)/size)+1;
        System.out.println(pageCount);
        if(page2>pageCount){
            page2=pageCount;
        }

//        //第一页放五条数据
//        int pages=(page2-1)*size;


        List<CKBus> list=ckBusService.querycheck((page2-1)*size,size,types);

//        System.out.println(list);
        //map存入不同类型对象
        Map map=new HashMap();
        map.put("page2",page2);
        map.put("count",count);
        map.put("pageCount",pageCount);
        map.put("list",list);


        //result对象返回数据到前端
        Result result=new Result();
        result.setData(map);
        return result;

    }


    //部件定检履历菜单查询
    @RequestMapping("/selectcheck")
    @ApiOperation(value = "整车定检履历分页查询")
    public Result selectcheck(@RequestParam(value = "types") String types){
        int page2=1;
        int size=5;

        //部件定检履历查出部件定检记录总条数
        int count=ckBusService.querycount(types);
        //求总页数
        int pageCount = count %size == 0 ? count / size : count / size + 1;



        //第一页放五条数据
        int pages=(page2-1)*size;
        List<CKBus> list=ckBusService.querycheck(pages,size,types);

        //map存入不同类型对象
        Map map=new HashMap();
        map.put("page2",page2);
        map.put("count",count);
        map.put("pageCount",pageCount);
        map.put("list",list);

        //result对象返回数据到前端
        Result result=new Result();
        result.setData(map);
        return result;
    }

    //搜索框输入条件查询
    @RequestMapping("/selectCKcontent")
    @ApiOperation(value = "搜索框输入条件查询")
    public Result selectCKcontent(@ApiParam(value = "checkid",required = false) String checkid){
        //搜索框条件查询可以是部件定检编号或者部件名称
        List<CKBus> list=ckBusService.querycheck2(checkid);

        //result返回查询出的部件定检履历记录
        Result result=new Result();
        result.setData(list);
        System.out.println(result);
        return result;


    }

    //新增按钮，整车定检履历新增
    @RequestMapping("/insertcheck")
    @ApiOperation(value = "新增按钮 整车定检履历新增")
    public void insertcheck(CKBus ckBus){
        //部件定检履历记录新增
        System.out.println(ckBus.getStarttime2());
        System.out.println(ckBus.getEndtime2());
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime ldt = LocalDate.parse(ckBus.getStarttime2(),df).atStartOfDay();
        LocalDateTime ldt2 = LocalDate.parse(ckBus.getEndtime2(),df).atStartOfDay();
        if(ckBus.getFailreason()==""){
            ckBus.setFailreason("无");
            ckBus.setCKstate("完成");

        }
        else {

            ckBus.setCKstate("失败");


        }
        ckBus.setStarttime(ldt);
        ckBus.setEndtime(ldt2);
        ckBus.setStatus(1);
        ckBus.setCheckid(MROUtils.nextId());
        ckBusService.insertcheck(ckBus);

    }


    @RequestMapping("/insertCKBus")
    public void insertTFBus(CKBus ckBus) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

        Date date = sdf.parse(ckBus.getStarttime2().split("\"")[1]);
//        System.out.println(date);
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();

        ckBus.setStarttime(instant.atZone(zoneId).toLocalDateTime());

        if (ckBus.getFailreason()==""){
            ckBus.setFailreason("无");
            ckBus.setCKstate("完成");
        }
        else {
            ckBus.setCKstate("失败");
        }
        Date date1 = new Date();
        Instant instant1 = date1.toInstant();
        ZoneId zoneId1 = ZoneId.systemDefault();

        ckBus.setEndtime(instant1.atZone(zoneId1).toLocalDateTime());
        ckBus.setStatus(1);

        System.out.println(ckBus);

        ckBusService.insertcheck(ckBus);
    }


    //编辑按钮，根据checkid查询出当前id的数据
    @RequestMapping("returncheck")
    @ApiOperation(value = "编辑按钮 查出要修改的数据")
    public Result returncheck(@RequestParam("checkid")Long checkid){
        List<CKBus>ckBuses=ckBusService.querycheckone(checkid);
        Result result=new Result();
        result.setData(ckBuses);
        return result;
    }

    //对根据checkid查询出的部件定检履历数据做修改
    @RequestMapping("/updatecheck")
    @ApiOperation(value = "整车定检履历数据修改")
    public void updatecheck(CKBus ckBus){

        if(ckBus.getFailreason()==""){

            ckBus.setCKstate("完成");
        }
        else {
            ckBus.setCKstate("失败");
        }

        ckBusService.updatecheck(ckBus);

    }

    //根据checkid删除修改这条记录的状态
    @RequestMapping("/deletecheck")
    @ApiOperation(value = "根据checkid删除修改这条记录的状态")
    public void deletecheck(@RequestParam("checkid")String checkid){
        //修改这条记录状态，0为消失，1为存在
       ckBusService.deletecheck(checkid);


    }

    @RequestMapping("/deletecheck2")
    public void deletetransform2(String checkid2){
        System.out.println(checkid2);
        String[] a=  checkid2.split(",");
        String[] c=  a[0].split("\\[");
        System.out.println(c[0]);
        System.out.println(c.length);
        String[] d = a[a.length-1].split("\\]");
        for (int i = 0; i < a.length; i++) {
            if (i==0){
                a[0]=c[1];

            }
            if (i==a.length-1){
                a[a.length-1]=d[0];
            }
            ckBusService.deletecheckone(a[i]);
        }
    }

}
